package com.hxk.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginLoginInterceptor implements HandlerInterceptor {
	//在进入控制器之前执行
		//如果返回值为false,阻止进入控制器
		//控制代码,比如登录控制
		@Override
		public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
			System.out.println("preHandle");
			String requestURI = arg0.getRequestURI();
			if(requestURI.indexOf("login") > -1){
				return true;
			}else{
				HttpSession session = arg0.getSession();
				Object attribute = session.getAttribute("user");
				if(attribute != null){
					return true;
				}else{
					arg1.sendRedirect("login.jsp");
					return false;
				}
			}
		}
		//控制器执行完成,进入到jsp之前执行.
		//日志记录.
		//敏感词语过滤
		@Override
		public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)throws Exception {
			System.out.println("postHandle");
		}
		//jsp执行完成后执行
		//记录执行过程中出现的异常.
		//可以把异常记录到日志中
		@Override
		public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)throws Exception {
			System.out.println("afterCompletion");
		}
}
